package domain;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class SecretHelper {

    CertificateHelper certificateHelper;
    public SecretHelper(CertificateHelper certificateHelper){
        this.certificateHelper=certificateHelper;
    }

    /**
     * 用公钥加密原文获取密文
     * @param orginStr 原文
     * @param name 证书别名
     * @param keyPsw 证书别名密码
     * @return
     * @throws Exception
     */
    public  String encrypt( String orginStr, String name, String keyPsw ) throws Exception{
        //base64编码的公钥
        String publicKeyStr=certificateHelper.getStrPublicKey(name,keyPsw);
        byte[] decoded = Base64.decodeBase64(publicKeyStr);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").
                generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(orginStr.getBytes("UTF-8")));
        return outStr;
    }
    /**
     * RSA私钥解密密文
     *
     * @param encrypedStr
     *            加密字符串
     * @param privateKey   私钥
     * @return
     * @throws Exception
     *             解密过程中的异常信息
     */
    public static String decrypt(String encrypedStr, String privateKey) throws Exception{
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(encrypedStr.getBytes("UTF-8"));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").
                generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }

    /**
     * RSA私钥解密密文
     *
     * @param encrypedStr
     *            加密字符串
     * @param name 证书别名
     * @param keyPsw 证书别名密码
     * @return
     * @throws Exception
     *             解密过程中的异常信息
     */
    public  String decrypt(String encrypedStr, String name,String keyPsw  ) throws Exception{
        //64位解码加密后的字符串
        String privateKey=certificateHelper.getStrPrivateKey(name,keyPsw);
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").
                generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        byte[] inputByte = Base64.decodeBase64(encrypedStr.getBytes("UTF-8"));
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }



}
